<?php

namespace app\common\model;

use app\admin\model\Githouse;
use think\Db;
use think\Model;

class Delivery extends Model
{

    // 表名
    protected $name = 'delivery_order';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
    ];

    public function DeliverGoods($where)
    {
        $where['status'] = 1;
        $row = $this->where($where)->field('group_concat(delivery_id) as cat_ids')->find();
        $data['list'] = Db::name('delivery_goods')->where(['delivery_id'=>['in',$row->cat_ids]])->select();
        $data['product'] = Db::name('delivery_goods')
            ->where(['delivery_id'=>['in',$row->cat_ids]])
            ->field('product_id,sum(number) as number')
            ->group('product_id')
            ->select();
        $data['list'] = Db::name('delivery_order')->where($where)->select();
        $Githouselist = Githouse::select();
        $Githouselist = collection($Githouselist)->toArray();
        $Githouselist = array_column($Githouselist,'gname','id');
        foreach ($data['list'] as &$value)
        {
            $value['goods'] = Db::name('delivery_goods')->where(['delivery_id'=>$value['delivery_id']])->select();
            if($value['type'] == 1 )
            {
                $value['warehouse_text'] = $Githouselist[$value['warehouse']];
            }else{
                $value['warehouse_text'] = Agent::AgentName($value['warehouse']);
            }
        }
        $data['product'] = array_column($data['product'],'number','product_id');
        return [$data['list'],$data['product']];
    }
}
